package com.lyk.coursearrange.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.common.core.base.result.R;
import com.lyk.coursearrange.common.log.annotation.Log;
import com.lyk.coursearrange.common.log.enums.OperationType;
import com.lyk.coursearrange.entity.RecruitmentInformation;
import com.lyk.coursearrange.entity.Template;
import com.lyk.coursearrange.entity.dto.RecruitmentInformationDto;
import com.lyk.coursearrange.service.RecruitmentInformationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/recruitmentInformation")
public class RecruitmentInformationController {

    @Autowired
    private RecruitmentInformationService recruitmentInformationService;

    /**
     * 添加招聘信息
     *
     * @param recruitmentInformation
     * @return
     */
    @PostMapping("/add")
    @Log(title = "添加招聘信息", operationType = OperationType.INSERT)
    public R<?> add(@RequestBody RecruitmentInformation recruitmentInformation) {

        recruitmentInformation.setStatus(1);
        boolean save = recruitmentInformationService.saveR(recruitmentInformation);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error("添加失败");
        }
    }
    @GetMapping("/getById/{id}")
    @Log(title = "单个查询招聘信息", operationType = OperationType.SELECT)
    public R getById(@PathVariable Long id) {
        LambdaQueryWrapper<RecruitmentInformation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RecruitmentInformation::getRecruitmentId,id);
        RecruitmentInformation recruitmentInformation = recruitmentInformationService.getOne(wrapper);
        return R.ok().data("info",recruitmentInformation);
    }

    @PostMapping("/page")
    @Log(title = "分页查询招聘列表", operationType = OperationType.SELECT)
    public R page(@RequestBody RecruitmentInformationDto r) {
        IPage<RecruitmentInformation> iPage = new Page<>(r.getPageNo(), r.getPageSize());
        LambdaQueryWrapper<RecruitmentInformation> wrapper = new LambdaQueryWrapper<>();
        if (r.getStatus() !=null){
            wrapper.eq(RecruitmentInformation::getStatus,r.getStatus());
        }
        wrapper.eq(RecruitmentInformation::getAdminNo,r.getAdminNo());
        IPage<RecruitmentInformation> page = recruitmentInformationService.page(iPage,wrapper);
        return R.ok().data("list",page);
    }

    @PutMapping("/update")
    @Log(title = "修改招聘信息", operationType = OperationType.UPDATE)
    public R<?> update(@RequestBody RecruitmentInformationDto r) {
        LambdaUpdateWrapper<RecruitmentInformation> wrapper = Wrappers.<RecruitmentInformation>lambdaUpdate()
                .set(StringUtils.isNotEmpty(r.getRecruitmentName()),RecruitmentInformation::getRecruitmentName,r.getRecruitmentName())
                .set(StringUtils.isNotEmpty(r.getRecruitmentPhone()),RecruitmentInformation::getRecruitmentPhone,r.getRecruitmentPhone())
                .set(StringUtils.isNotEmpty(r.getRecruitmentUrl()),RecruitmentInformation::getRecruitmentUrl,r.getRecruitmentUrl())
                .set(StringUtils.isNotEmpty(r.getPosition()),RecruitmentInformation::getPosition,r.getPosition())
                .set(RecruitmentInformation::getStatus,r.getStatus())
                .set(RecruitmentInformation::getRecord,r.getRecord())
                .eq(RecruitmentInformation::getRecruitmentId,r.getRecruitmentId());
        boolean result = recruitmentInformationService.update(wrapper);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error("保存失败");
        }
    }
}
